<template>
    <div class="app-container">
        <!--查询数据-->
        <div class="SearchTop">
            <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch">
                <el-form-item label="套餐名称：" prop="name">
                    <el-input v-model="queryParams.name" placeholder="请输入套餐名称" clearable @keyup.enter="handleQuery" />
                </el-form-item>
                <el-form-item label="计费模式：" prop="billingModel">
                    <el-select v-model="queryParams.billingModel" placeholder="全部" clearable>
                        <el-option v-for="dict in combo_billing_model" :key="dict.value" :label="dict.label"
                            :value="dict.value" />
                    </el-select>
                </el-form-item>
                <el-form-item label="套餐状态：" prop="status">
                    <el-select v-model="queryParams.status" placeholder="全部" clearable>
                        <el-option v-for="dict in tenant_combo_status" :key="dict.value" :label="dict.label"
                            :value="dict.value" />
                    </el-select>
                </el-form-item>

                <el-row class="searchBtn">
                    <el-col :span="8">
                        <el-form-item>
                            <el-button type="primary" @click="handleQuery" icon="Search"
                                v-hasPermi="['tenant:combo:list']">查询
                            </el-button>
                            <el-button icon="Refresh" plain @click="resetQuery">重置</el-button>
                        </el-form-item>
                    </el-col>
                    <el-col :span="5">
                        <el-form-item>
                            <el-button type="primary" icon="Plus" @click="handleAdd" v-hasPermi="['tenant:combo:add']">
                                新增套餐</el-button>
                            <el-button @click="handleExport" type="info" plain icon="Download"
                                v-hasPermi="['tenant:combo:export']">
                                导出</el-button>
                            <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete"
                                v-hasPermi="['tenant:combo:remove']">删除</el-button>
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
            <showUp :showSearch="showSearch" @showUp="packUp"></showUp>
        </div>

        <!--表格数据-->
        <div class="tableList">
            <el-tabs v-model="activeName" class="demo-tabs" @tab-click="handleClick">
                <el-tab-pane label="全部" name="0"></el-tab-pane>
                <el-tab-pane label="主套餐" name="1"></el-tab-pane>
                <el-tab-pane label="辅助套餐" name="2"></el-tab-pane>
            </el-tabs>
            <el-table :data="tableData" v-loading="loading" style="width: 100%" border class="table-container" stripe
                align="center" @selection-change="handleSelectionChange">
                <el-table-column type="selection" width="55" :align="center"> </el-table-column>
                <el-table-column label="序号" width="60" type="index" :align="center"> </el-table-column>
                <template v-for="(item, i) in tableHead" :key="i">
                    <el-table-column :prop="item.dataIndex" :label="item.title" show-overflow-tooltip :align="center"
                        class-name="release">
                        <template #default="scope" v-if="item.dataIndex == 'status'">
                            <div v-if="scope.row.status == 1">
                                <span class="greenF">已发布</span>
                            </div>
                            <div v-if="scope.row.status == 0">
                                <span class="orange">未发布</span>
                            </div>
                        </template>
                    </el-table-column>
                </template>
                <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                    <template #default="scope">
                        <a @click="handleUpdate(scope.row)" title="编辑" v-hasPermi="['tenant:combo:edit']">
                            <img src="../../../assets/images/manage/bianji.png" alt="" />

                        </a>
                        <a v-if="scope.row.status == 0" title="发布" @click="handleRelease(scope.row)">
                            <img src="../../../assets/images/manage/fabu.png" alt="" />
                        </a>
                        <a v-if="scope.row.status == 1" title="撤销发布" @click="Unpublish(scope.row)">
                            <img src="../../../assets/images/manage/cexiao.png" alt="" />
                        </a>
                        <a @click="handleDelete(scope.row)" title="删除" v-hasPermi="['tenant:combo:remove']">
                            <img src="../../../assets/images/manage/shanchu.png" alt="" />
                        </a>
                    </template>
                </el-table-column>
            </el-table>
            <pagination v-show="total > 0" v-model:page="queryParams.pageNum" :total="total"
                v-model:limit="queryParams.pageSize" @pagination="getList" />
        </div>
        <addCombo ref="addFrom" @refresh="getList"></addCombo>
    </div>
</template>
<script setup >
import addCombo from './addCombo'
import showUp from '@/components/packUp'
import { listCombo, delCombo, linkCombo, updateComboStatus } from '@/api/tenant/combo'
const { proxy } = getCurrentInstance();
const { tenant_combo_status, combo_billing_model } = proxy.useDict("tenant_combo_status", "combo_billing_model");
const activeName = ref('0')
const showSearch = ref(true);
const loading = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const data = reactive({
    form: {},
    queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: undefined,
        billingModel: undefined,
        status: undefined
    },

    tableHead: [
        {
            dataIndex: 'name',
            title: '套餐名称',

        },
        {
            dataIndex: 'type_dictText',
            title: '套餐类型',

        },
        {
            dataIndex: 'billingModel_dictText',
            title: '计费模式',

        },
        {
            dataIndex: 'cost',
            title: '套餐价格',

        },
        {
            dataIndex: 'status',
            title: '套餐状态',

        }

    ],
    tableData: []
});
const { queryParams, form, rules, tableHead, tableData } = toRefs(data);
//收起查询栏
function packUp() {
    showSearch.value = !showSearch.value

}
/** 查询列表 */
function getList() {
    loading.value = true;
    listCombo(queryParams.value).then(response => {
        if (response.code == 200) {
            tableData.value = response.rows
            total.value = response.total;
            loading.value = false;
        }
    })
}
//套餐切换列表查询
function handleClick(tab, event) {
    if (tab.index == '0') {
        queryParams.value.type = ''
    }
    if (tab.index == '1') {
        queryParams.value.type = '0'
    }
    if (tab.index == '2') {
        queryParams.value.type = '1'
    }
    getList()
}
/** 搜索按钮操作 */
function handleQuery() {
    queryParams.value.pageNum = 1;
    getList();
}
/** 重置按钮操作 */
function resetQuery() {
    queryParams.value = {}
    handleQuery();
}
//发布
function handleRelease(row) {
    const status = 1;
    const comboId = row.id;
    proxy.$modal.confirm('是否要发布套餐？').then(function () {
        return updateComboStatus(comboId, status)
    }).then(() => {
        getList();
        proxy.$modal.msgSuccess("发布成功");
    }).catch(() => { });
}
//判断套餐是否关联租户
function linkTenant(comboId) {
    linkCombo(comboId).then(res => {
        if (res.code == 200) {
            if (res.data == false) {
                const status = 0
                proxy.$modal.confirm('是否撤销发布此套餐？').then(function () {
                    return updateComboStatus(comboId, status)
                }).then(() => {
                    getList();
                    proxy.$modal.msgSuccess("撤销发布成功");
                }).catch(() => { });
            }
            if (res.data == true) {
                proxy.$modal.alert('套餐已关联租户，不能撤销发布')
            }
        }
    })
}
//撤销发布
function Unpublish(row) {
    const comboIds = row.id;
    linkTenant(comboIds)
}
/** 删除按钮操作 */
function handleDelete(row) {
    const comboIds = row.id || ids.value;
    proxy.$modal.confirm('是否确认删除此套餐？').then(function () {
        return delCombo(comboIds);
    }).then(() => {
        getList();
        proxy.$modal.msgSuccess("删除成功");
    }).catch(() => { });
}
/** 多选框选中数据 */
function handleSelectionChange(selection) {
    ids.value = selection.map(item => item.id);
    single.value = selection.length != 1;
    multiple.value = !selection.length;
}
function handleUpdate(row) {
    if (row.status == 1) {
        proxy.$modal.msgError("套餐已发布，不能编辑！");
    } else {
        proxy.$refs["addFrom"].edit(row);
    }

}
async function handleAdd() {
    // visible.value = true;
    proxy.$refs["addFrom"].add();
}
/** 导出按钮操作 */
function handleExport() {
    proxy.download("/tenant/combo/export", {
        ...queryParams.value,
    }, `套餐_${new Date().getTime()}.xlsx`);
};
getList()
</script>